package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ActivityManagementMapper;
import com.ruoyi.system.mapper.ActivityUserSignatureMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.service.IActivityUserSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 活动签到Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-02
 */
@Service
public class ActivityUserSignatureServiceImpl implements IActivityUserSignatureService
{
    @Autowired
    private ActivityUserSignatureMapper activityUserSignatureMapper;
    @Autowired
    private SysPostMapper sysPostMapper;
    @Autowired
    private ActivityManagementMapper activityManagementMapper;

    /**
     * 查询活动签到
     *
     * @param id 活动签到主键
     * @return 活动签到
     */
    @Override
    public ActivityUserSignature selectActivityUserSignatureById(Long id)
    {
        return activityUserSignatureMapper.selectActivityUserSignatureById(id);
    }

    /**
     * 查询活动签到列表
     *
     * @param activityUserSignature 活动签到
     * @return 活动签到
     */
    @Override
    public List<ActivityUserSignature> selectActivityUserSignatureList(ActivityUserSignature activityUserSignature)
    {
        List<ActivityUserSignature> activityUserSignatureList = activityUserSignatureMapper.selectActivityUserSignatureList(activityUserSignature);
        for(ActivityUserSignature signature : activityUserSignatureList){
            String userPostId = signature.getUserPostId();
            if(StringUtils.isNotBlank(userPostId)){
                List<String> userPostIdList = Arrays.asList(userPostId.split(","));
                List<String> postNameList = new ArrayList();
                for(String postId : userPostIdList){
                    SysPost sysPost = sysPostMapper.selectPostById(Long.valueOf(postId));
                    postNameList.add(sysPost.getPostName());
                }
                signature.setPostNameList(postNameList);
            }
            String status = signature.getStatus();
            //迟到
            if("3".equals(status)){
                //签到时间
                String signatureTime = signature.getSignatureTime();
                Date signatureTimeDate = DateUtil.parse(signatureTime, "yyyy-MM-dd HH:mm:ss");
                ActivityManagement activityManagement = activityManagementMapper.selectActivityManagementById(activityUserSignature.getId());
                String activityStartTime = activityManagement.getActivityStartTime();
                Date activityStartTimeDate = DateUtil.parse(activityStartTime, "yyyy-MM-dd HH:mm:ss");
                //活动开始5分钟之后算时间
                Date startDate = DateUtil.offsetMinute(activityStartTimeDate, 5);
                //计算迟到时间
                long between = DateUtil.between(signatureTimeDate, startDate, DateUnit.MINUTE);
                signature.setLayTime((int) between);
            }
        }
        return activityUserSignatureList;
    }

    /**
     * 新增活动签到
     *
     * @param activityUserSignature 活动签到
     * @return 结果
     */
    @Override
    public int insertActivityUserSignature(ActivityUserSignature activityUserSignature)
    {
        activityUserSignature.setCreateTime(DateUtils.getNowDate());
        List<String> postIdList = activityUserSignature.getPostIdList();
        String postId = StringUtils.join(postIdList, ",");
        activityUserSignature.setUserPostId(postId);
        return activityUserSignatureMapper.insertActivityUserSignature(activityUserSignature);
    }

    /**
     * 修改活动签到
     *
     * @param activityUserSignature 活动签到
     * @return 结果
     */
    @Override
    public AjaxResult updateActivityUserSignature(ActivityUserSignature activityUserSignature)
    {
        activityUserSignature.setUpdateTime(DateUtils.getNowDate());
        ActivityUserSignature activityUserSignatureVo = activityUserSignatureMapper.selectActivityUserSignatureById(activityUserSignature.getId());
        ActivityManagement activityManagement = activityManagementMapper.selectActivityManagementById(activityUserSignatureVo.getActivityId());
        String activityStartTime = activityManagement.getActivityStartTime();
        Date activityStartTimeDate = DateUtil.parse(activityStartTime, "yyyy-MM-dd HH:mm:ss");
        //活动开始前15分钟签到开始
        Date startDate = DateUtil.offsetMinute(activityStartTimeDate, -15);
        //活动结束 签到结束
        String ActivityEndTime = activityManagement.getActivityEndTime();
        Date ActivityEndTimeDate = DateUtil.parse(ActivityEndTime, "yyyy-MM-dd HH:mm:ss");
        Date nowDate = DateUtils.getNowDate();
        boolean in = DateUtil.isIn(nowDate, startDate, ActivityEndTimeDate);
        if(!in){
            return AjaxResult.error("不在签到时间范围");
        }
        //签到时间
        activityUserSignature.setSignatureTime(DateUtil.format(nowDate,"yyyy-MM-dd HH:mm:ss"));
        //活动开始5分钟之后算迟到
        Date layDate = DateUtil.offsetMinute(activityStartTimeDate, 5);
        int compare = DateUtil.compare(layDate, nowDate);
        if(compare>0){
            //正常签到
            activityUserSignature.setStatus("1");
        }else{
            //迟到
            activityUserSignature.setStatus("3");
        }

        activityUserSignatureMapper.updateActivityUserSignature(activityUserSignature);
        return AjaxResult.success("签到成功");
    }

    /**
     * 删除活动签到信息
     *
     * @param id 活动签到主键
     * @return 结果
     */
    @Override
    public int deleteActivityUserSignatureById(Long id)
    {
        return activityUserSignatureMapper.deleteActivityUserSignatureById(id);
    }

    /**
     * 根据活动类型查询人员签到次数
     *
     * @param activityUserSignature 活动签到
     * @return 活动签到集合
     */
    @Override
    public List<ActivityUserSignature> selectUserSignatureCountByActivityType(ActivityUserSignature activityUserSignature){
        return activityUserSignatureMapper.selectUserSignatureCountByActivityType(activityUserSignature);
    }
}
